<?php

namespace app\admin\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;


//资金流水日志
class Statistics extends Base
{
    public  $model;

    public function __construct()
    {

        parent::__construct();

    }

    /**
     * @return \think\response\Json  询价历史
     */
    public function index()
    {//

        //统计数据
        $Users = new \app\admin\model\Users;

        $UsersRecharge = new \app\admin\model\UsersRecharge;//提现 充值
        $OrderDetails = new \app\admin\model\OrderDetails;
        $AdminAgent = new \app\admin\model\AdminAgent;
        $AdminStaff = new \app\admin\model\AdminStaff;
        $data = [];
        //今日注册人数
        $whereStirng = " DATE_FORMAT(curdate(),'%Y%m%d') = FROM_UNIXTIME(created_at, '%Y%m%d') ";
        $UpeStirng = " DATE_FORMAT(curdate(),'%Y%m%d') = FROM_UNIXTIME(update_at, '%Y%m%d') ";   //今日注册人数
        $whereStirng2 = " DATE_FORMAT(curdate(),'%Y%m%d') = FROM_UNIXTIME(a.created_at, '%Y%m%d') ";
        $UpeStirng2 = " DATE_FORMAT(curdate(),'%Y%m%d') = FROM_UNIXTIME(a.update_at, '%Y%m%d') ";
        $whereBase = [];
        //今日买入金额

        if($this->agents_ids=="all"){//管理员组

            $data['UserCount'] = $Users->where($whereStirng)->count();//今日注册人数
            $data['UserCz'] = $UsersRecharge->where($whereStirng)->where($whereBase)->where("type",1)->count();//充值
            $data['UserTx'] = $UsersRecharge->where($whereStirng)->where($whereBase)->where("type",2)->count();//提现

            $data['UserBuyMoney'] = $OrderDetails->where($UpeStirng)->where($whereBase)->where("status",2)->sum("manage_money");//进入买入
            $data['UserSellMoney'] = $OrderDetails->where($UpeStirng)->where($whereBase)->where("is_sell",1)->sum("manage_money");//进入卖出
        }else{

            $whereBase['u.agnet_id'] = ["in",$this->agents_ids];
            $whereBase2['a.agnet_id'] = ["in",$this->agents_ids];

            $data['UserCount'] = $Users->where($whereStirng2)->where($whereBase2)
                ->join("admin_agent ag","ag.id=a.agent_id","left")->count();
            $data['UserCz'] = $UsersRecharge->where($whereStirng2)->where($whereBase2)
                ->join("users u","u.id=a.user_id","left")
                ->join("admin_agent ag","ag.id=u.agent_id","left")->where("type",1)->count();
            $data['UserTx'] = $UsersRecharge->where($whereStirng2)->where($whereBase2)
                ->join("users u","u.id=a.user_id","left")
                ->join("admin_agent ag","ag.id=u.agent_id","left")->where("type",2)->count();

            $data['UserBuyMoney'] = $OrderDetails->where($UpeStirng2)->where($whereBase2)->where("status",2)
                ->join("users u","u.id=a.user_id","left")
                ->join("admin_agent ag","ag.id=u.agent_id","left")
                ->sum("manage_money");
            $data['UserSellMoney'] = $OrderDetails->where($UpeStirng2)->where($whereBase2)->where("is_sell",1)
                ->join("users u","u.id=a.user_id","left")
                ->join("admin_agent ag","ag.id=u.agent_id","left")
                ->sum("manage_money");

/*
            $data['UserCount'] = $Users->where($whereStirng)->count();//今日注册人数
            $data['UserCz'] = $UsersRecharge->where($whereStirng)->where($whereBase)->where("type",1)->count();//充值
            $data['UserTx'] = $UsersRecharge->where($whereStirng)->where($whereBase)->where("type",2)->count();//提现

            $data['UserBuyMoney'] = $OrderDetails->where($UpeStirng)->where($whereBase)->where("status",2)->sum("manage_money");//进入买入
            $data['UserSellMoney'] = $OrderDetails->where($UpeStirng)->where($whereBase)->where("is_sell",1)->sum("manage_money");//进入卖出*/
        }




        //当前账户信息
        if($this->sidRows['is_manger']){
                    //搜索代理商的信息
            $money  =$AdminAgent->where("id",$this->sidRows['agent_id'])->find();
            $data['CurrMoney'] = $money['money'];

        }else{
            //搜索员工的信息
            $money  =$AdminStaff->where("id",$this->sidRows['id'])->find();
            $data['CurrMoney'] = $money['money'];//当前值
        }


        return sucessJson("查询成功",$data);


    }









}
